package com.sky.controller.admin;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.service.impl.EmployeeServiceImpl;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags="和员工相关的接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;  //登录才需要使用到

    //一般查询类的设置Result返回的泛型 非查询类的一般返回只有个状态码 不设置也没事

    /**
     * 保存员工的信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("保存修改的员工信息")
    public Result SaveEmployeeInfomation(@RequestBody EmployeeDTO employeeDTO){
        employeeService.UpdateInfo(employeeDTO);
        return Result.success();
    }

    /**
     * 根据员工的id进行信息查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("根据员工ID查询信息")
    public Result<Employee> GetEmployeeById(@PathVariable("id") Long id){
        Employee employee = employeeService.queryById(id);
        return Result.success(employee);
    }

    /**
     * 设置员工的状态信息 1为启用 0为禁用
     * @param status
     * @param id
     * @return
     */
    @PostMapping("status/{status}")  //由于同名 PathVariable后面的括号也可以不写
    @ApiOperation("员工状态设置")
    public Result SetEmployeeStatus(@PathVariable("status") Integer status,Long id){
        log.info("开始对员工状态进行修改 {}:{}",status,id);
        employeeService.SetStatus(status,id);
        return Result.success();
    }


    /**
     * 员工注册
     */
    @PostMapping
    @ApiOperation("新增员工的接口 注册接口")  //get employeeDTO from the EmployeeDTO,use RequestBody to transfer json to java class type
    public Result register(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工的功能: {}",employeeDTO);
        //use the service layers to address
        employeeService.save(employeeDTO);
        return Result.success();
    }

    /**
     * 查询employee的员工信息
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工信息查询")
    public Result<PageResult> query(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("查询分页信息:{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.query(employeePageQueryDTO);
//        System.out.println(pageResult);

        //由于后台管理中 没有权限更改用户密码 同时为了避免密码的md5泄露 直接禁止显示到前端
        //后端手动拦截输出到前端的字段中的password 设置其为空
//        List<Employee> employees = pageResult.getRecords();
//        System.out.println(employees);
//        if(employees != null){
//            for(Employee employee : employees){
//                //方法1：直接设置为空
//                employee.setPassword(null);
//            }
//        }
//        System.out.println(employees);

        //方法2：直接在employee中设置了@JsonIgnore字段注解

        return Result.success(pageResult);
    }


    /**
     * 登录
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录的功能")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        //EmployeeLoginVO是要返回给前端的内容
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()  //通过配合@Builder注解 将对象的属性值注入进去
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出的功能")  //value属性可以不写 是默认的
    public Result<String> logout() {
        return Result.success();
    }

}
